/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.view.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *  This class is responsible for convert all end line and formating from input string
 * to html tags.
 * @author Tom
 * @version 1.0.0. beta
 */
public class StringFormatter implements Converter{

    public StringFormatter() {
    }
    /**
     * This method replace all tags <code>\r\n</code> to <code> br/ </code> the 
     * source is string in parameter.
     * 
     * @param fc faces context
     * @param uic component
     * @param string string in which will be replaced all tags <code>\r\n</code> to <code> br/ </code>
     * @return string in which are replaced all tags <code>\r\n</code> to <code> br/ </code>.
     */
    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if(string==null){
            return string;
        }
        else{
            string=string.replaceAll("\r\n", "<br/>");
            return string;
        }
    }
     /**
     * This method replace all tags <code>\r\n</code> to <code> br/ </code> the 
     * source is string in parameter.
     * 
     * @param fc faces context
     * @param uic component
     * @param o object from which will be taken string, in which will be replaced 
     * all tags <code>\r\n</code> to <code> br/ </code>
     * @return string in which are replaced all tags <code>\r\n</code> to <code> br/ </code>.
     */
    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        String line=(String) o;
        if(line==null){
            return line;
        }
        else{
            line=line.replaceAll("\r\n", "<br/>");
            return line;
        }
    }
    
}
